/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.DetermineLogin;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.Article;
import com.mxg.blog.entity.ArticleCategory;
import com.mxg.blog.service.ArticleCategoryService;
import com.mxg.blog.service.ArticleService;
import com.mxg.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/23 18:03
 * 获取回收站的文章内容
 */
@Controller
public class GetRecycleBinArticle {
    CqLogger logger = CqLogger.getInstance();

    private final ArticleService articleService;
    private final UserService userService;
    private final ArticleCategoryService articleCategoryService;

    public GetRecycleBinArticle(ArticleService articleService,
                             UserService userService,
                             ArticleCategoryService articleCategoryService)
    {
        this.articleService = articleService;
        this.userService = userService;
        this.articleCategoryService = articleCategoryService;
    }

    @PostMapping("api/recycleBinArticle")
    public ResponseEntity<Map<String, Object>> getRecycleBinArticle(@RequestBody Map<String, String> requestMap) {
        Map<String, Object> resultMap = new HashMap<>();

        // 获取用户Id
        Integer userId;

        DetermineLogin determineLogin = DetermineLogin.getInstance(requestMap.get("userAccount"), userService);

        if(determineLogin.startJudge().equals(DetermineLogin.NOT_LOGIN)){
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            return ResponseEntity.ok(resultMap);
        }else{
            userId = determineLogin.startJudge();
        }

        logger.print(userId);

        // 获取文章列表
        List<Article> articleList = articleService.getArticleByUserIdAndStatus0(userId);

        //logger.print(articleList);

        // 判断获取的文章是否存在
        if(!articleList.isEmpty()){
            List<Map<String, Object>> tableData = new ArrayList<>();

            // 将获取的文章保存进返回值
            for(Article article : articleList) {
                Map<String, Object> articleInfo = new HashMap<>();

                articleInfo.put(ResultCode.id, article.getArticleId());
                articleInfo.put(ResultCode.title, article.getArticleTitle());
                articleInfo.put(ResultCode.articleImages, article.getArticleImages());
                articleInfo.put(ResultCode.articleExcerpt, article.getArticleExcerpt());
                articleInfo.put(ResultCode.articleTags, article.getArticleTags());

                // 是否有分类
                if (article.getArticleCategoryId() != null) {
                    ArticleCategory articleCategory =  articleCategoryService.getArticleCategoryNameById(article.getArticleCategoryId());

                    articleInfo.put(ResultCode.articleCategory, articleCategory.getArticleCategoryName());

                } else {
                    articleInfo.put(ResultCode.articleCategory, "未分类");
                }
                articleInfo.put(ResultCode.articleUpdated, article.getArticleUpdatedAt());
                logger.print(1);

                tableData.add(articleInfo);
            }
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.tableData, tableData);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);

            logger.print("回收站数据获取成功!");

            return ResponseEntity.ok(resultMap);
        }else{
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_HAVE_NO_THIS_ARTICLE);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_HAVE_NO_THIS_ARTICLE);
        }
        return ResponseEntity.ok(resultMap);
    }
}
